﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DisplayBoardInfo.Server.Entities
{
    public class Appointment : Entity
    {
        Model.Appointment appointment;
        bool newItem = false;

        /// <summary>
        /// Creates new, empty entity.
        /// </summary>
        public Appointment()
        {
            this.appointment = new Model.Appointment();
            newItem = true;
        }

        /// <summary>
        /// Creates appointment with specyfic ID.
        /// </summary>
        /// <param name="ID">ID of appointment.</param>
        /// <exception cref="ArgumentOutOfRange">Appointment with specyfic ID does not exist.</exception>
        public Appointment(int ID)
        {
            IEnumerable<Model.Appointment> appointments = db.Appointments.Where(i=>i.ID == ID);
            if (appointments.Count() > 0)
            {
                appointment = appointments.First();
            }
            else throw new ArgumentOutOfRangeException("Appointment with that ID does not exist.");
        }

        /// <summary>
        /// Refresh all data in entity from database.
        /// </summary>
        public override void Refresh()
        {
            db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, this.appointment);
        }

        /// <summary>
        /// Saves changes in database.
        /// </summary>
        public override void Save()
        {
            if(newItem) db.Appointments.InsertOnSubmit(appointment);

            db.SubmitChanges();
        }

        /// <summary>
        /// Gets appointment ID.
        /// </summary>
        public int ID
        {
            get
            {
                return appointment.ID;
            }
        }

        /// <summary>
        /// Gets or sets patient who is connected with current appointment.
        /// </summary>
        public Patient Patient
        {
            get
            {
                return new Patient(this.appointment.PatientID);
            }
            set 
            {
                if (newItem)
                {
                    Model.Patient p = new Model.Patient();
                    p.LastName = value.LastName;
                    p.Name = value.Name;
                    p.PESEL = p.PESEL;
                    this.appointment.Patient = p;

                }
                else this.appointment.PatientID = value.ID;
            }
        }

        /// <summary>
        /// Gets or sets doctor who is connected with current appointment.
        /// </summary>
        /// <exception cref="ArgumentException">User does not be in \"Lekarz\" role.</exception>
        public User Doctor
        {
            get
            {
                return new User(this.appointment.Doctor);
            }

            set
            {
                //if (value.Role.Name == "Lekarz")
                //{
                    this.appointment.Doctor = value.Username;
                //}
                //else throw new ArgumentException("User have to be in \"Lekarz\" role.");
            }
        }

        /// <summary>
        /// Gets or sets date and time of appointment.
        /// </summary>
        public DateTime DateTime
        {
            get
            {
                return this.appointment.DateTime;
            }

            set
            {
                this.appointment.DateTime = value;
            }
        }

        /// <summary>
        /// Gets or sets position in queue.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Number is smaller than 1.</exception>
        public short? Number
        {
            get
            {
                return this.appointment.Number;
            }

            set
            {
                if (value > 0)
                {
                    this.appointment.Number = value;
                }
                else throw new ArgumentOutOfRangeException("Number has to be grater than 0");
            }
        }

        /// <summary>
        /// Gets or sets appointment length
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">Number is smaller than 1.</exception>
        public int? Time
        {
            get
            {
                return this.appointment.Time;
            }

            set
            {
                if (value > 0)
                {
                    this.appointment.Time = value;
                }
                else throw new ArgumentOutOfRangeException("Number has to be grater than 0");
            }
        }
    }
}
